import React from 'react';
import {Spin, Card, Button, Table} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import PropTypes from 'prop-types';
import {partOrderPlanHisStatus} from '../Enum';
import routes from './routes';
import {DATE_FORMAT, TABLE} from '../constants';
import {conventEnumValueToString, formatDateTime} from '../utils';
import {getHistoryDetail} from './api';
import {injectIntl} from './intl';
import moment from 'moment';
const {Description} = DescriptionList;

class HistoryDetailPanel extends React.PureComponent {
    state = {
        isFetching: false,
        data: {},
        details: [],
        partSaleOrder: {}
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({isFetching: true});
        getHistoryDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        isFetching: false,
                        data: res.data,
                        partOrderPlanHisDetails: res.data.partOrderPlanHisDetails,
                        partOrderPlanHisProcessDetails: res.data.partOrderPlanHisProcessDetails
                    });
                else
                    this.setState(({
                        isFetching: false
                    }));
            });
    }

    onCancel = () => {
        this.props.history.push(routes.historyQuery.url());
    }

    render() {
        const {data, partOrderPlanHisDetails, partOrderPlanHisProcessDetails} = this.state;
        const {formatMessage} = this.props.intl;
        let totalQuantity = 0;
        if(partOrderPlanHisDetails)
            partOrderPlanHisDetails.forEach(d => {
                totalQuantity += (d.orderQuantity);
            });
        const detailColums = [{
            title: formatMessage({
                id: 'historyDetailPanel.column.partCode',
                defaultMessage: '备件编号'
            }),
            dataIndex: 'partCode',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.partName',
                defaultMessage: '备件名称'
            }),
            dataIndex: 'partName',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.partEngName',
                defaultMessage: '备件英文名称'
            }),
            dataIndex: 'partEngName'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.productCategoryCode',
                defaultMessage: '适用车型'
            }),
            dataIndex: 'productCategoryCode'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.minPackSize',
                defaultMessage: '最小包装数'
            }),
            dataIndex: 'minPackSize'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.orderQuantity',
                defaultMessage: '订购数量'
            }),
            dataIndex: 'orderQuantity',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.remark',
                defaultMessage: '备注'
            }),
            dataIndex: 'remark'
        }];
        const operateColums = [{
            title: formatMessage({
                id: 'historyDetailPanel.column.processer',
                defaultMessage: '操作人'
            }),
            dataIndex: 'processer'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.processStatus',
                defaultMessage: '操作状态'
            }),
            dataIndex: 'processStatus',
            render: text => conventEnumValueToString(partOrderPlanHisStatus, text)
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.processComment',
                defaultMessage: '操作意见'
            }),
            dataIndex: 'processComment',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.processDate',
                defaultMessage: '操作时间'
            }),
            dataIndex: 'processDate',
        }];
        return (
            <div>
                <Spin spinning={this.state.isFetching}>
                    <Card>
                        <DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.card.partOrderPlanHistory',
                            defaultMessage: '备件需求计划历史详情'
                        })}>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.dealerCode',
                                defaultMessage: '总代号'
                            })}>{data.dealerCode}
                            </Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.createTime',
                                defaultMessage: '提报日期'
                            })}>{formatDateTime(data.createTime, DATE_FORMAT)}</Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.country',
                                defaultMessage: '国家'
                            })}>{data.country}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.code',
                                defaultMessage: '需求计划编号'
                            })}>{data.code}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.status',
                                defaultMessage: '状态'
                            })}>{conventEnumValueToString(partOrderPlanHisStatus, data.status)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.planYearmonth',
                                defaultMessage: '需求计划年月'
                            })}>{moment(data.planYearmonth).format('YYYY-MM')}</Description>
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.table.details',
                            defaultMessage: '需求计划清单'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={detailColums}
                                rowKey="partId"
                                pagination={false}
                                dataSource={partOrderPlanHisDetails}
                                footer={() => <DescriptionList>
                                    <Description term={formatMessage({
                                        id: 'newPanel.operation.totalQuantity',
                                        defaultMessage: '总数量'
                                    })}>{totalQuantity}
                                    </Description>
                                </DescriptionList>}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.table.operateDetails',
                            defaultMessage: '操作历史清单'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={operateColums}
                                rowKey="processer"
                                pagination={false}
                                dataSource={partOrderPlanHisProcessDetails}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" onClick={this.onCancel}>{formatMessage({
                        id: 'historyDetailPanel.operation.onCancel',
                        defaultMessage: '返回'
                    })}</Button>
                </div>
            </div>
        );
    }
}

HistoryDetailPanel.propTypes = {
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object
};

export default injectIntl(HistoryDetailPanel);
